package cn.est.service.impl;

import cn.est.config.OssConfig;
import cn.est.dao.ModelMapper;
import cn.est.pojo.Model;
import cn.est.query.ModelQuery;
import cn.est.service.ModelService;
import cn.est.vo.ModelVO;
import cn.est.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
@EnableConfigurationProperties(OssConfig.class)
public class ModelServiceImpl implements ModelService {
    @Autowired
    private ModelMapper modelMapper;
    @Autowired
    private OssConfig ossConfig;
    @Override
    public List<Model> listUpModelByClassifyIdAndBrandId(Long classifyId, Long brandId) {
        return modelMapper.listUpModelByClassifyIdAndBrandId(classifyId,brandId);
    }

    @Override
    public PageVO<ModelVO> listByPage(ModelQuery modelQuery) {
        int count = modelMapper.countByModelQuery(modelQuery);
        List<Model> modelList = modelMapper.listUpByModelQuery(modelQuery);
        List<ModelVO> modelVOList = new ArrayList<>();
        for (Model model : modelList) {
            ModelVO modelVO = new ModelVO();
            BeanUtils.copyProperties(model,modelVO);
            modelVO.setFaceImg("//"+ossConfig.getOssWebUrl()+"/"+modelVO.getFaceImg());
            modelVOList.add(modelVO);
        }
        PageVO<ModelVO> pageVO = new PageVO<>();
        pageVO.setPageNo(modelQuery.getPageNo());
        pageVO.setPageSize(modelQuery.getPageSize());
        pageVO.setTotal(count);
        pageVO.setList(modelVOList);
        return pageVO;
    }

    @Override
    public ModelVO getModelById(Long id) {
        Model model = modelMapper.selectByPrimaryKey(id);
        ModelVO modelVO = new ModelVO();
        BeanUtils.copyProperties(model,modelVO);
        modelVO.setFaceImg("//"+ossConfig.getOssWebUrl()+"/"+modelVO.getFaceImg());
        modelVO.setContentImg("//"+ossConfig.getOssWebUrl()+"/"+modelVO.getContentImg());
        return modelVO;
    }
}
